package com.hw.one.peach.core.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


import com.hw.one.core.auth.context.SystemContextHolder;
import com.hw.one.core.controller.PageController;
import com.hw.one.core.result.MsgResult;
import com.hw.one.monitor.service.ISiteService;
import com.hw.one.peach.core.service.IEncircledAreaService;
import com.hw.one.peach.core.service.IMapPointService;
import com.hw.one.util.DateTimeUtil;
import com.hw.one.util.UUIDUtil;

/**
  * @author  sjd 
  * @date 创建时间：2017年7月28日 下午1:56:16 
  * @version 1.0.0
  */
@RestController
@RequestMapping(path = "area")
public class EncircledAreaController extends PageController {
	
	@Resource
	private ISiteService siteService;
	
	@Resource
	private IEncircledAreaService encircledAreaService;
	
	@Resource
	private IMapPointService mapPointService;
	
	
	private Logger log = LoggerFactory.getLogger(EncircledAreaController.class);
	
	/**
	 * 根据行政区域编码和点位类型 和点位类别查询点位信息这个方法主要查郑州的
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "findSitesByRegionCodeAndSiteType", method = RequestMethod.POST)
	public MsgResult findSiteInfoByRegionCodeAndSiteType(HttpServletRequest request) {
		Map<String, Object> params = new HashMap<String,Object>();
		String regionCode ="410100000";//代表郑州      nationalSite：代表国控空气站
		
		params.put("regionCode",regionCode);
		params.put("siteType","nationalSite");  
		params.put("siteCategory","");
		params.put("siteName","");
		try {
			return siteService.findSiteInfoByRegionCodeAndSiteType(params);
		}catch (Exception e) {
			log.error("查询站点信息", e);
			return MsgResult.exceptionResult();
		}

	}
	

	
	@Override
	public MsgResult add(HttpServletRequest request){
		Map<String,Object> params = wrapParams(request);
		String siteCode = (String)params.get("siteCode");
		params.put("nationalSite", "nationalSite");
		String areaCode="";
		
		if (!StringUtils.isBlank(siteCode)){
			areaCode = siteCode + "001";
		}
		params.put("areaCode", areaCode);
		
		String pointAddressStr = (String)params.get("pointAddress");
		String lonStr = (String)params.get("lon");
		String latStr = (String)params.get("lat");
		String orderNumStr = (String)params.get("orderNum");
		if(!StringUtils.isBlank(pointAddressStr)&&!StringUtils.isBlank(lonStr)&&!StringUtils.isBlank(lonStr)){
			String[] pointAddresses = pointAddressStr.split(",");
			String[] lons = lonStr.split(",");
			String[] lats = latStr.split(",");
			String[] orderNums = orderNumStr.split(",");
			
			List<Map<String,Object>> pointList = new ArrayList<>();
			Map<String,Object> point = null;
			if(pointAddresses.length==lons.length&&lons.length==lats.length){
				for(int i=0;i<pointAddresses.length;i++){
					if(!StringUtils.isBlank(pointAddresses[i])&&!StringUtils.isBlank(lons[i])&&!StringUtils.isBlank(lats[i])){
						point = new HashMap<String,Object>();
						point.put("id", UUIDUtil.getUUID());
						point.put("areaCode", areaCode);
						point.put("pointAddress", pointAddresses[i]);
						point.put("longitude", lons[i]);
						point.put("latitude", lats[i]);
						point.put("creator", SystemContextHolder.getAccountId());
						point.put("createDate", DateTimeUtil.getCurrentDate());
						point.put("editor", SystemContextHolder.getAccountId());
						point.put("modifiedDate", DateTimeUtil.getCurrentDate());
						point.put("orderNum", orderNums[i]);
						pointList.add(point);
					}else{
						return MsgResult.exceptionResult("点位数据添加有空值");
					}
					
				}
				params.put("pointList", pointList);
			}else{
				return MsgResult.exceptionResult("点位数据添加有空值");
			}
		}else{
			return MsgResult.exceptionResult("点位数据添加有空值");
		}
		
		Map<String,Object> map = encircledAreaService.findEncircledAreaBySiteCode(params);
		
		if(map == null){
			try{
				return encircledAreaService.add(params);
			}catch(Exception e){
				log.error("add EncircledArea fail"+e.getMessage());
				return MsgResult.exceptionResult();
			}
			
		}else{
			return MsgResult.exceptionResult("此站点已经存在合围区");
		}
		
		
	}
	
	@Override
	public MsgResult edit(HttpServletRequest request, @PathVariable String id) {
		Map<String, Object> params = wrapParams(request);
		params.put("id", id);
		String siteCode = (String)params.get("siteCode");
		Map<String,Object> areaMap;
		String areaCode="";
		if(!StringUtils.isBlank(siteCode)){
			areaMap = encircledAreaService.findEncircledAreaBySiteCode(params);
			mapPointService.deletes(areaMap);
			areaCode = (String)areaMap.get("areaCode");
		}
		String pointAddressStr = (String)params.get("pointAddress");
		String lonStr = (String)params.get("lon");
		String latStr = (String)params.get("lat");
		String orderNumStr = (String)params.get("orderNum");
		if(!StringUtils.isBlank(pointAddressStr)&&!StringUtils.isBlank(lonStr)&&!StringUtils.isBlank(lonStr)){
			String[] pointAddresses = pointAddressStr.split(",");
			String[] lons = lonStr.split(",");
			String[] lats = latStr.split(",");
			String[] orderNums = orderNumStr.split(",");
			List<Map<String,Object>> pointList = new ArrayList<>();
			Map<String,Object> point = null;
			if(pointAddresses.length==lons.length&&lons.length==lats.length){
				for(int i=0;i<pointAddresses.length;i++){
					if(!StringUtils.isBlank(pointAddresses[i])&&!StringUtils.isBlank(lons[i])&&!StringUtils.isBlank(lats[i])){
						point = new HashMap<String,Object>();
							point.put("id", UUIDUtil.getUUID());
							point.put("areaCode", areaCode);
							point.put("pointAddress", pointAddresses[i]);
							point.put("longitude", lons[i]);
							point.put("latitude", lats[i]);
							point.put("creator", SystemContextHolder.getAccountId());
							point.put("createDate", DateTimeUtil.getCurrentDate());
							point.put("editor", SystemContextHolder.getAccountId());
							point.put("modifiedDate", DateTimeUtil.getCurrentDate());
							point.put("orderNum", orderNums[i]);
							pointList.add(point);
						
					}else{
						return MsgResult.exceptionResult("点位数据添加有空值");
					}
					
				}
				params.put("pointList", pointList);
				
				
				
			}else{
				return MsgResult.exceptionResult("点位数据添加有空值");
			}
		}else{
			return MsgResult.exceptionResult("点位数据添加有空值");
		}
		

		try {
			// 需要验证时添加
			return encircledAreaService.edit(params);
		} catch (Exception e) {
			log.error("账户修改", e);
			return MsgResult.exceptionResult("出现异常");
		}
	}
	
	
	@Override
	public MsgResult getById(HttpServletRequest request, @PathVariable String id) {
		
		if(!StringUtils.isBlank(id)){
			try {
				return encircledAreaService.getById(id);
			} catch (Exception e) {
				log.error("修改合围区", e);
				return MsgResult.exceptionResult();
			}
		}else{
			return MsgResult.exceptionResult("请选择一条记录");
		}
		
		
		
	}
	
	 @Override
	public MsgResult delete(HttpServletRequest request, @PathVariable String id) {
			try {
				return encircledAreaService.delete(id);
			} catch (Exception e) {
				log.error("账户客户端失败", e);
				return MsgResult.exceptionResult();
			}
		}
	
	 
	 @RequestMapping(value = "/deletes", method = RequestMethod.POST)
	public MsgResult deletes(HttpServletRequest request) {
			Map<String, Object> params = wrapParams(request);
			try {
				return encircledAreaService.deletes(params);
			} catch (Exception e) {
				log.error("批量删除客户端信息", e);
				return MsgResult.exceptionResult();
			}
		}
	 
	

}
